package com.fancon.android.gardenjokesx.social;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.fancon.android.gardenjokesx.R;
import com.fancon.android.gardenjokesx.entity.FacebookUser;
import com.fancon.android.gardenjokesx.entity.FilterLock;
import com.fancon.android.gardenjokesx.entity.FlickrUser;
import com.fancon.android.gardenjokesx.entity.TumblrUser;
import com.fancon.android.gardenjokesx.entity.TwitterUser;
import com.fancon.android.gardenjokesx.entity.User;
import com.fancon.android.gardenjokesx.entity.WeiboUser;
import com.fancon.android.gardenjokesx.utils.Constant;

public class DatabaseHelper extends SQLiteOpenHelper {

	static final String DBname = "gardenjokes.sqlite";
	private Context mContext;

	public DatabaseHelper(Context context) {
		super(context, DBname, null, 3);
		// TODO Auto-generated constructor stub
		mContext = context;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub

		db.execSQL("CREATE TABLE tblAlbumSetting (UserId VARCHAR(100) PRIMARY KEY  NOT NULL  UNIQUE , SaveOrgImage CHAR, SaveProcessedImage CHAR)");
		db.execSQL("CREATE TABLE tblFacebook (UserId VARCHAR(100), FacebookId VARCHAR(300), AccessToken VARCHAR(300), Username VARCHAR(100))");
		db.execSQL("CREATE TABLE tblFilter (FilterId INTEGER PRIMARY KEY  NOT NULL  DEFAULT (0) ,Filtername VARCHAR(100))");
		db.execSQL("CREATE TABLE tblFlickr (UserId VARCHAR(100), FlickrId VARCHAR(300), AccessToken VARCHAR(300), Username VARCHAR(100))");
		db.execSQL("CREATE TABLE tblLockedFilter (UserId VARCHAR(100) NOT NULL , FilterId INTEGER NOT NULL , PennantName VARCHAR(100), PRIMARY KEY (UserId, FilterId))");
		db.execSQL("CREATE TABLE tblTumblr (UserId VARCHAR(100) PRIMARY KEY  NOT NULL , Username VARCHAR(100), Password VARCHAR(100))");
		db.execSQL("CREATE TABLE tblTwitter (UserId VARCHAR(100) PRIMARY KEY  NOT NULL ,TwitterToken VARCHAR(300),TwitterSecret VARCHAR(300),TwitterServiceId VARCHAR(100),TwitterUsername VARCHAR(100),TwitterVerifier VARCHAR(100))");
		db.execSQL("CREATE TABLE tblUser (UserId VARCHAR(100) PRIMARY KEY  NOT NULL  UNIQUE , Username VARCHAR(100), Password VARCHAR(100))");
		db.execSQL("CREATE TABLE tblWeibo (UserId VARCHAR(100) PRIMARY KEY  NOT NULL ,WeiboToken VARCHAR(300),WeiboSecret VARCHAR(300),ServiceId VARCHAR(100),WeiboVerifier VARCHAR(100), Username VARCHAR(100))");
		

		// insert tblFilter data
		insertFilterData(db);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		db.execSQL("DROP TABLE IF EXISTS tblAlbumSetting");
		db.execSQL("DROP TABLE IF EXISTS tblFacebook");
		db.execSQL("DROP TABLE IF EXISTS tblFilter");
		db.execSQL("DROP TABLE IF EXISTS tblFlickr");
		db.execSQL("DROP TABLE IF EXISTS tblLockedFilter");
		db.execSQL("DROP TABLE IF EXISTS tblTumblr");
		db.execSQL("DROP TABLE IF EXISTS tblTwitter");
		db.execSQL("DROP TABLE IF EXISTS tblUser");
		db.execSQL("DROP TABLE IF EXISTS tblWeibo");
		onCreate(db);
	}

	private void insertFilterData(SQLiteDatabase db) {
		String[] filterData = mContext.getResources().getStringArray(
				R.array.db_filter);
		ContentValues cv = new ContentValues();

		for (int i = 0; i < filterData.length; i++) {
			cv.put("FilterId", i);
			cv.put("Filtername", filterData[i]);
			db.insert("tblFilter", "Filtername", cv);
		}
	}

	public boolean isFlickrLinked(String userId) {
		SQLiteDatabase db = this.getWritableDatabase();
		String[] str = new String[] { userId };

		Cursor cur = db.rawQuery(
				"SELECT UserId from tblFlickr WHERE UserId = ?", str);
		if (cur != null) {
			int i = cur.getCount();
			cur.close();
			db.close();
			if (i > 0) {
				return true;
			} else
				return false;
		} else {
			db.close();
			return false;
		}
	}

	public boolean isTumblrLinked(String userId) {
		SQLiteDatabase db = this.getWritableDatabase();
		String[] str = new String[] { userId };

		Cursor cur = db.rawQuery(
				"SELECT UserId from tblTumblr WHERE UserId = ?", str);
		if (cur != null) {
			int i = cur.getCount();
			cur.close();
			db.close();
			if (i > 0) {
				return true;
			} else
				return false;
		} else {
			db.close();
			return false;
		}
	}

	// WEIBO
	public boolean isWeiboLinked(String userId) {
		SQLiteDatabase db = this.getWritableDatabase();
		String[] str = new String[] { userId };

		Cursor cur = db.rawQuery(
				"SELECT UserId from tblWeibo WHERE UserId = ?", str);
		if(cur != null){
			int i = cur.getCount();
			cur.close();
			db.close();
			if (i > 0) {
				return true;
			} else
				return false;
			}
		else{
			db.close();
			return false;
		}
	}

	// FACEBOOK
	public boolean isFacebookLinked(String userId) {
		SQLiteDatabase db = this.getWritableDatabase();
		String[] str = new String[] { userId };
		Log.d("UserID","User ID : " + userId+ "  " + str[0]);
		 Cursor cur =
		 db.rawQuery("SELECT UserId from tblFacebook WHERE UserId = ?", str);
		if (cur != null) {
			int i = cur.getCount();
			cur.close();
			db.close();
			if (i > 0) {
				return true;
			} else
				return false;
		} else {
			db.close();
			return false;
		}
	}

	/************************************* ALBUM SETTING ******************************/

	// ALBUM SETTING
	public boolean isSettingAlbum(String userId) {
		SQLiteDatabase db = this.getWritableDatabase();
		String[] str = new String[] { userId };

		Cursor cur = db.rawQuery(
				"SELECT UserId from tblAlbumSetting WHERE UserId = ?", str);
		if(cur != null){
			int i = cur.getCount();
			cur.close();
			db.close();
			if (i > 0) {
				return true;
			} else
				return false;
		}
		else{
			db.close();
			return false;
		}
	}

	/**
	 * 
	 * @param userId
	 * @param SaveOrgImage
	 *            : String "0" or "1"
	 * @param SaveProcessedImage
	 */
	public void addAlbumSetting(String userId, String SaveOrgImage,
			String SaveProcessedImage) {
		ContentValues cv = new ContentValues();
		SQLiteDatabase db = this.getWritableDatabase();

		cv.put("UserId", userId);
		cv.put("SaveOrgImage", SaveOrgImage);
		cv.put("SaveProcessedImage", SaveProcessedImage);

		db.insert("tblAlbumSetting ", null, cv);
		// db.rawQuery("INSERT INTO tblAlbumSetting(UserId, SaveOrgImage, SaveProcessedImage) values("+userId+","+SaveOrgImage+","+SaveProcessedImage+")",
		// null);
		db.close();
	}

	/**
	 * 
	 * @param userId
	 * @param SaveOrgImage
	 * @param SaveProcessedImage
	 */
	public void updateAlbumSetting(String userId, String SaveOrgImage,
			String SaveProcessedImage) {
		ContentValues cv = new ContentValues();
		SQLiteDatabase db = this.getWritableDatabase();
		String[] str = new String[] { userId };
		cv.put("UserId", userId);
		cv.put("SaveOrgImage", SaveOrgImage);
		cv.put("SaveProcessedImage", SaveProcessedImage);

		db.update("tblAlbumSetting ", cv, "UserId = ?", str);
		db.close();
	}

	/**
	 * Get Twitter information from local database by userId.
	 * 
	 * @param userId
	 * @return
	 */
	public Cursor getAlbumSetting(String userId) {
		SQLiteDatabase db = this.getWritableDatabase();
		String[] str = new String[] { userId };
		Cursor cur = db
				.rawQuery(
						"SELECT UserId, SaveOrgImage, SaveProcessedImage FROM tblAlbumSetting WHERE UserId = ?",
						str);
		return cur;
	}
	
	
	public boolean getSaveAlbumSetting(String userId) {
		boolean save = true;
		SQLiteDatabase db = this.getWritableDatabase();
		String[] str = new String[] { userId };
		Cursor cur = db
				.rawQuery(
						"SELECT UserId, SaveOrgImage, SaveProcessedImage FROM tblAlbumSetting WHERE UserId = ?",
						str);
		if(cur != null){
			int i = cur.getCount();
			db.close();
			if (i > 0) {
				cur.moveToFirst();
				String saveAlbum  = cur.getString(cur.getColumnIndex("SaveProcessedImage"));
				if (saveAlbum.equals(Constant.NOT_PUBLIC)) {
					save = false;
				}
			} 
			cur.close();
		}
		
		return save;
	}

	/**
	 * 
	 * @param userId
	 */
	public void deleteAlbumSetting(String userId) {

		SQLiteDatabase db = this.getWritableDatabase();
		db.delete("tblAlbumSetting ", "UserId = ?", new String[] { userId });
		db.close();
	}

	/************************************** TWITTER ************************************/
	public boolean isTwitterLinked(String userId) {
		SQLiteDatabase db = this.getWritableDatabase();
		String[] str = new String[] { userId };

		Cursor cur = db.rawQuery(
				"SELECT UserId from tblTwitter WHERE UserId = ?", str);
		if(cur != null){
			int i = cur.getCount();
			cur.close();
			db.close();
			if (i > 0) {
				return true;
			} else
				return false;
		}else {
			db.close();
			return false;
		}
	}

	/**
	 * Add user information into database.
	 * 
	 * @param userId
	 *            (String)
	 * @param twitterToken
	 *            (String)
	 * @param twitterSecret
	 *            (String)
	 * @param twitterServiceId
	 *            (String)
	 * @param twitterUsername
	 *            (String)
	 * @param twitterVerifier
	 *            (String)
	 */
	public void addTwitterUser(String userId, String twitterToken,
			String twitterSecret, String twitterServiceId,
			String twitterUsername, String twitterVerifier) {
		ContentValues cv = new ContentValues();
		SQLiteDatabase db = this.getWritableDatabase();

		cv.put("UserId", userId);
		cv.put("TwitterToken", twitterToken);
		cv.put("TwitterSecret", twitterSecret);
		cv.put("TwitterServiceId", twitterServiceId);
		cv.put("TwitterUsername", twitterUsername);
		cv.put("TwitterVerifier", twitterVerifier);

		db.insert("tblTwitter", null, cv);
		// db.rawQuery("INSERT INTO tblTwitter(UserId, TwitterToken, TwitterSecret, TwitterServiceId, TwitterUsername, TwitterVerifier) values('sdfds-23', 'sdfds-34535-sdffsd', 'secret', 'service', 'user name', 'verifier')",
		// null);
		db.close();
	}

	/**
	 * Update user information into database.
	 * 
	 * @param userId
	 *            (String)
	 * @param twitterToken
	 *            (String)
	 * @param twitterSecret
	 *            (String)
	 * @param twitterServiceId
	 *            (String)
	 * @param twitterUsername
	 *            (String)
	 * @param twitterVerifier
	 *            (String)
	 */
	public void updateTwitterUser(String userId, String twitterToken,
			String twitterSecret, String twitterServiceId,
			String twitterUsername, String twitterVerifier) {
		ContentValues cv = new ContentValues();
		SQLiteDatabase db = this.getWritableDatabase();
		String[] str = new String[] { userId };
		cv.put("UserId", userId);
		cv.put("TwitterToken", twitterToken);
		cv.put("TwitterSecret", twitterSecret);
		cv.put("TwitterServiceId", twitterServiceId);
		cv.put("TwitterUsername", twitterUsername);
		cv.put("TwitterVerifier", twitterVerifier);

		db.update("tblTwitter", cv, "UserId = ?", str);
		db.close();
	}

	/**
	 * Get Twitter information from local database by userId.
	 * 
	 * @param userId
	 * @return
	 */
	public TwitterUser getTwitterUser(String userId) {
		TwitterUser twUser = new TwitterUser();
		
		SQLiteDatabase db = this.getWritableDatabase();
		String[] str = new String[] { userId };
		Cursor cur = db
				.rawQuery(
						"SELECT UserId, TwitterToken, TwitterSecret, TwitterServiceId,"
								+ " TwitterUsername, TwitterVerifier FROM tblTwitter WHERE UserId = ?",
						str);
		if (cur == null){
			twUser = null;
		}
		else if(cur.getCount() > 0){
			cur.moveToFirst();
			twUser.setUserId(cur.getString(cur.getColumnIndex("UserId")));
			twUser.setTwitterToken(cur.getString(cur.getColumnIndex("TwitterToken")));
			twUser.setTwitterSecret(cur.getString(cur.getColumnIndex("TwitterSecret")));
			twUser.setTwitterServiceId(cur.getString(cur.getColumnIndex("TwitterServiceId")));
			twUser.setTwitterUsername(cur.getString(cur.getColumnIndex("TwitterUsername")));
			twUser.setTwitterVerifier(cur.getString(cur.getColumnIndex("TwitterVerifier")));
		}else{
			twUser = null;
		}

		cur.close();
		db.close();
		return twUser;
	}

	public void deleteTwitterUser(String userId) {

		SQLiteDatabase db = this.getWritableDatabase();
		db.delete("tblTwitter", "UserId = ?", new String[] { userId });
		db.close();
	}

	// WEIBO
	/**
	 * 
	 * @param userId
	 * @param WeiboToken
	 * @param WeiboSecret
	 * @param ServiceId
	 * @param WeiboVerifier
	 * @param Username
	 */
	public void addWeiboUser(String userId, String WeiboToken,
			String WeiboSecret, String ServiceId, String WeiboVerifier,
			String Username) {
		ContentValues cv = new ContentValues();
		SQLiteDatabase db = this.getWritableDatabase();

		cv.put("UserId", userId);
		cv.put("WeiboToken", WeiboToken);
		cv.put("WeiboSecret", WeiboSecret);
		cv.put("ServiceId", ServiceId);
		cv.put("WeiboVerifier", WeiboVerifier);
		cv.put("Username", Username);

		db.insert("tblWeibo", null, cv);
		// db.rawQuery("INSERT INTO tblTwitter(UserId, TwitterToken, TwitterSecret, TwitterServiceId, TwitterUsername, TwitterVerifier) values('sdfds-23', 'sdfds-34535-sdffsd', 'secret', 'service', 'user name', 'verifier')",
		// null);
		db.close();
	}

	/**
	 * 
	 * @param userId
	 * @param WeiboToken
	 * @param WeiboSecret
	 * @param ServiceId
	 * @param WeiboVerifier
	 * @param Username
	 */
	public void updateWeiboUser(String userId, String WeiboToken,
			String WeiboSecret, String ServiceId, String WeiboVerifier,
			String Username) {
		ContentValues cv = new ContentValues();
		SQLiteDatabase db = this.getWritableDatabase();
		String[] str = new String[] { userId };
		cv.put("UserId", userId);
		cv.put("WeiboToken", WeiboToken);
		cv.put("WeiboSecret", WeiboSecret);
		cv.put("ServiceId", ServiceId);
		cv.put("WeiboVerifier", WeiboVerifier);
		cv.put("Username", Username);

		db.update("tblWeibo", cv, "UserId = ?", str);
		db.close();
	}

	/**
	 * 
	 * @param userId
	 * @return
	 */
	public WeiboUser getWeiboUser(String userId) {
		WeiboUser user = new WeiboUser();
		
		SQLiteDatabase db = this.getWritableDatabase();
		String[] str = new String[] { userId };
		Cursor cur = db
				.rawQuery(
						"SELECT UserId, WeiboToken, WeiboSecret, ServiceId,"
								+ " WeiboVerifier, Username FROM tblWeibo WHERE UserId = ?",
						str);
		
		if(cur == null)
			user = null;
		else if(cur.getCount() > 0){
			cur.moveToFirst();
			user.setUserId(cur.getString(cur.getColumnIndex("UserId")));
			user.setServiceId(cur.getString(cur.getColumnIndex("ServiceId")));
			user.setWeiboSecret(cur.getString(cur.getColumnIndex("WeiboSecret")));
			user.setWeiboToken(cur.getString(cur.getColumnIndex("WeiboToken")));
			user.setWeiboVerifier(cur.getString(cur.getColumnIndex("WeiboVerifier")));
			user.setUsername(cur.getString(cur.getColumnIndex("Username")));
		}else{
			user = null;
		}
		cur.close();
		db.close();
		
		return user;
	}

	/**
	 * 
	 * @param userId
	 */
	public void deleteWeiboUser(String userId) {

		SQLiteDatabase db = this.getWritableDatabase();
		db.delete("tblWeibo", "UserId = ?", new String[] { userId });
		db.close();
	}

	// FACEBOOK.
	/**
	 * 
	 * @param userId
	 * @param FacebookId
	 * @param AccessToken
	 * @param Username
	 */
	public void addFacebookUser(String userId, String FacebookId,
			String AccessToken, String Username) {
		ContentValues cv = new ContentValues();
		SQLiteDatabase db = this.getWritableDatabase();

		cv.put("UserId", userId);
		cv.put("FacebookId", FacebookId);
		cv.put("AccessToken", AccessToken);
		cv.put("Username", Username);

		db.insert("tblFacebook", null, cv);
		// db.rawQuery("INSERT INTO tblTwitter(UserId, TwitterToken, TwitterSecret, TwitterServiceId, TwitterUsername, TwitterVerifier) values('sdfds-23', 'sdfds-34535-sdffsd', 'secret', 'service', 'user name', 'verifier')",
		// null);
		db.close();
	}

	/**
	 * 
	 * @param userId
	 * @param FacebookId
	 * @param AccessToken
	 * @param Username
	 */
	public void updateFacebookUser(String userId, String FacebookId,
			String AccessToken, String Username) {
		ContentValues cv = new ContentValues();
		SQLiteDatabase db = this.getWritableDatabase();
		String[] str = new String[] { userId };

		cv.put("UserId", userId);
		cv.put("FacebookId", FacebookId);
		cv.put("AccessToken", AccessToken);
		cv.put("Username", Username);

		db.update("tblFacebook", cv, "UserId = ?", str);
		db.close();
	}

	/**
	 * 
	 * @param userId
	 * @return
	 */
	public FacebookUser getFacebookUser(String userId) {

		FacebookUser fbUser = new FacebookUser();
		SQLiteDatabase db = this.getWritableDatabase();
		String[] str = new String[] { userId };
		Cursor cur = db
				.rawQuery(
						"SELECT UserId, FacebookId, AccessToken, Username FROM tblFacebook WHERE UserId = ?",
						str);
		if(cur == null){
			fbUser = null;
		}
		else if(cur.getCount() > 0){
			cur.moveToFirst();
			fbUser.setUserId(cur.getString(cur.getColumnIndex("UserId")));
			fbUser.setFacebookId(cur.getString(cur.getColumnIndex("FacebookId")));
			fbUser.setAccessToken(cur.getString(cur.getColumnIndex("AccessToken")));
			fbUser.setUsername(cur.getString(cur.getColumnIndex("Username")));
		}else{
			fbUser = null;
		}
		cur.close();
		db.close();
		
		return fbUser;
	}

	/**
	 * 
	 * @param userId
	 */
	public void deleteFacebookUser(String userId) {

		SQLiteDatabase db = this.getWritableDatabase();
		db.delete("tblFacebook", "UserId = ?", new String[] { userId });
		db.close();
	}

	// FLICKR
	/**
	 * 
	 * @param userId
	 * @param FlickrId
	 * @param AccessToken
	 * @param Username
	 */
	public void addFlickrUser(String userId, String FlickrId,
			String AccessToken, String Username) {
		ContentValues cv = new ContentValues();
		SQLiteDatabase db = this.getWritableDatabase();

		cv.put("UserId", userId);
		cv.put("FlickrId", FlickrId);
		cv.put("AccessToken", AccessToken);
		cv.put("Username", Username);

		db.insert("tblFlickr", null, cv);
		// db.rawQuery("INSERT INTO tblTwitter(UserId, TwitterToken, TwitterSecret, TwitterServiceId, TwitterUsername, TwitterVerifier) values('sdfds-23', 'sdfds-34535-sdffsd', 'secret', 'service', 'user name', 'verifier')",
		// null);
		db.close();
	}

	/**
	 * 
	 * @param userId
	 * @param FlickrId
	 * @param AccessToken
	 * @param Username
	 */
	public void updateFlickrUser(String userId, String FlickrId,
			String AccessToken, String Username) {
		ContentValues cv = new ContentValues();
		SQLiteDatabase db = this.getWritableDatabase();
		String[] str = new String[] { userId };

		cv.put("UserId", userId);
		cv.put("FlickrId", FlickrId);
		cv.put("AccessToken", AccessToken);
		cv.put("Username", Username);

		db.update("tblFlickr", cv, "UserId = ?", str);
		db.close();
	}

	/**
	 * 
	 * @param userId
	 * @return
	 */
	public FlickrUser getFlickrUser(String userId) {
		FlickrUser user = new FlickrUser();
		
		SQLiteDatabase db = this.getWritableDatabase();
		String[] str = new String[] { userId };
		Cursor cur = db
				.rawQuery(
						"SELECT UserId, FlickrId, AccessToken, Username FROM tblFlickr WHERE UserId = ?",
						str);
		if(cur == null){
			user = null;
		}
		if(cur.getCount() > 0){
			cur.moveToFirst();
			user.setUserId(cur.getString(cur.getColumnIndex("UserId")));
			user.setUsername(cur.getString(cur.getColumnIndex("Username")));
			user.setFlickrId(cur.getString(cur.getColumnIndex("FlickrId")));
			user.setAccessToken(cur.getString(cur.getColumnIndex("AccessToken")));
		}else{
			user = null;
		}
		cur.close();
		db.close();
		
		return user;
	}

	/**
	 * 
	 * @param userId
	 */
	public void deleteFlickrUser(String userId) {

		SQLiteDatabase db = this.getWritableDatabase();
		db.delete("tblFlickr", "UserId = ?", new String[] { userId });
		db.close();
	}

	// TUMBLR
	/**
	 * 
	 * @param userId
	 * @param Username
	 * @param Password
	 */
	public void addTumblrUser(String userId, String Username, String Password) {
		ContentValues cv = new ContentValues();
		SQLiteDatabase db = this.getWritableDatabase();

		cv.put("UserId", userId);
		cv.put("Username", Username);
		cv.put("Password", Password);

		db.insert("tblTumblr", null, cv);
		// db.rawQuery("INSERT INTO tblTwitter(UserId, TwitterToken, TwitterSecret, TwitterServiceId, TwitterUsername, TwitterVerifier) values('sdfds-23', 'sdfds-34535-sdffsd', 'secret', 'service', 'user name', 'verifier')",
		// null);
		db.close();
	}

	/**
	 * 
	 * @param userId
	 * @param Username
	 * @param Password
	 */
	public void updateTumblrUser(String userId, String Username, String Password) {
		ContentValues cv = new ContentValues();
		SQLiteDatabase db = this.getWritableDatabase();
		String[] str = new String[] { userId };

		cv.put("UserId", userId);
		cv.put("Username", Username);
		cv.put("Password", Password);

		db.update("tblTumblr", cv, "UserId = ?", str);
		db.close();
	}

	/**
	 * 
	 * @param userId
	 * @return
	 */
	public TumblrUser getTumblrUser(String userId) {
		TumblrUser user = new TumblrUser();
		
		SQLiteDatabase db = this.getWritableDatabase();
		String[] str = new String[] { userId };
		Cursor cur = db
				.rawQuery(
						"SELECT UserId, Username, Password FROM tblTumblr WHERE UserId = ?",
						str);
		
		if (cur == null){
			user = null;
		}
		else if(cur.getCount() > 0){
			cur.moveToFirst();
			user.setUserId(cur.getString(cur.getColumnIndex("UserId")));
			user.setUsername(cur.getString(cur.getColumnIndex("Username")));
			user.setPassword(cur.getString(cur.getColumnIndex("Password")));
		}else{
			user = null;
		}
		cur.close();
		db.close();
		
		return user;
	}

	/**
	 * 
	 * @param userId
	 */
	public void deleteTumblrUser(String userId) {

		SQLiteDatabase db = this.getWritableDatabase();
		db.delete("tblTumblr", "UserId = ?", new String[] { userId });
		db.close();
	}

	// USER.
	public void addUser(User user) {

		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues cv = new ContentValues();

		cv.put("UserId", user.getUser_id());
		cv.put("Username", user.getUser_name());
		cv.put("Password", user.getPassword());

		db.insert("tblUser", null, cv);
		db.close();
	}
	
	/**********************************************FLITER LOCK*********************************************************/
	
	public ArrayList<FilterLock> getFilterLock(){
		ArrayList<FilterLock> filter = new ArrayList<FilterLock>();
		SQLiteDatabase db = this.getWritableDatabase();
		
		Cursor cur = db.rawQuery("Select * from tblLockedFilter ", null);
		if(cur == null){
			db.close();
			return null;
		}else if(cur.getCount() > 0){
			cur.moveToFirst();
			while(!cur.isAfterLast()){
				FilterLock f = new FilterLock();
				f.setFilter_id(cur.getInt(cur.getColumnIndex("FilterId")));
				f.setPennant_name(cur.getString(cur.getColumnIndex("PennantName")));
				Log.e("database","Filter name : " + cur.getInt(cur.getColumnIndex("FilterId")));
				filter.add(f);
				f = null;
				cur.moveToNext();
			}
			cur.close();
			db.close();
			return filter;
		}else{
			cur.close();
			db.close();
			return filter;
		}
	}
	
	public void addFilterLock(ArrayList<FilterLock> filter, String user_id){
		SQLiteDatabase db = this.getWritableDatabase();
		
		ContentValues cv = new ContentValues();
		Log.d("database","data size: "+ filter.size());
		for(int i = 0; i < filter.size(); i++){
			Log.d("database","filter id: "+ filter.get(i).getFilter_id()+", penant: "+filter.get(i).getPennant_name());
			cv.put("UserId", user_id);
			cv.put("FilterId", filter.get(i).getFilter_id());
			cv.put("PennantName", filter.get(i).getPennant_name());
			db.insert("tblLockedFilter", null, cv);
		}
		
		db.close();
		return ;
	}
	
	public void updateFilterLock(ArrayList<FilterLock> filter, String user_id){
		SQLiteDatabase db = this.getWritableDatabase();

		db.execSQL("DROP TABLE IF EXISTS tblLockedFilter");
		db.execSQL("CREATE TABLE tblLockedFilter (UserId VARCHAR(100) NOT NULL , FilterId INTEGER NOT NULL , PennantName VARCHAR(100), PRIMARY KEY (UserId, FilterId))");
		db.close();
		
		addFilterLock(filter, user_id);
		
	}
}
